1 package ch.odi.justblog.api.blogger;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.Map;
9 import java.util.Vector;
10
11 import org.apache.xmlrpc.XmlRpcClient;
12 import org.apache.xmlrpc.XmlRpcException;
13
14 import ch.odi.justblog.api.ApiException;
15
16 /***
17 * Facade for the Blogger API.
18 *
19 * First set the credentials, call getUserInfo and getUsersBlogs.
20 * Then set the desired Blog.
21 * Now call newPost, editPost, getTemplate and setTemplate.
22 *
23 * @author oglueck
24 */
25 public class Blogger {
26
27 public static final String TEMPLATE_TYPE_MAIN = "main";
28 public static final String TEMPLATE_TYPE_ARCHIVE = "archiveIndex";
29
30 private URL url;
31 private String blogId;
32 private String username="", password="";
33 /*** obtained through http://www.blogger.com/developers/api/1_docs/register.html" target="alexandria_uri">http://www.blogger.com/developers/api/1_docs/register.html */
34 private static final String appkey = "1f17ffffff8bffffff9f0516ffffff8524ffffff9dffffff9650fffffff3ffffff972affffffce1cffffffaf48ffffffd1ffffffb112ffffffe048ffffffc1";
35
36 /***
37 *
38 * @param url URL of the XML-RPC2 endpoint.
39 */
40 public Blogger(URL url) {
41 this.url = url;
42 }
43
44 public void setBlog(BloggerBlog blog) {
45 blogId = blog.getId();
46 }
47
48 /***
49 * Sets the user credentials. It is allowed to change the credentials at any time.
50 *
51 * @param username
52 * @param password
53 */
54 public void setCredentials(String username, char[] password) {
55 this.username = username;
56 this.password = new String(password);
57 }
58
59 /***
60 * Makes a new post to a designated blog. Optionally, will publish the blog after making the post.
61 * A blog must be set before calling this method.
62 *
63 * @param posting The content to publish
64 * @param publish if true the content if made publicly available immediately
65 * @return the new ID of the content
66 * @throws IOException
67 * @throws ApiException
68 */
69 public String newPost(String posting, boolean publish) throws IOException, ApiException {
70 XmlRpcClient client = new XmlRpcClient(url);
71 Vector params = new Vector();
72 params.addElement(appkey);
73 params.addElement(blogId);
74 params.addElement(username);
75 params.addElement(password);
76 params.addElement(posting);
77 params.addElement(Boolean.valueOf(publish));
78 String id = null;
79 try {
80 Object result = client.execute("blogger.newPost", params);
81 if (result instanceof String) {
82 id = (String) result;
83 } else {
84 throw new ApiException("Expected String result but got "+ result.getClass().getName());
85 }
86 } catch (XmlRpcException e) {
87 throw new ApiException(e);
88 }
89 return id;
90 }
91
92 /***
93 * Edits a given post. Optionally, will publish the blog after making the edit.
94 * A blog must be set before calling this method.
95 *
96 * @param id
97 * @param posting
98 * @param publish
99 * @throws IOException
100 * @throws ApiException
101 */
102 public void editPost(String id, String posting, boolean publish) throws IOException, ApiException {
103 XmlRpcClient client = new XmlRpcClient(url);
104 Vector params = new Vector();
105 params.addElement(appkey);
106 params.addElement(id);
107 params.addElement(username);
108 params.addElement(password);
109 params.addElement(posting);
110 params.addElement(Boolean.valueOf(publish));
111 try {
112 Object result = client.execute("blogger.editPost", params);
113 boolean ok = false;
114 if (result instanceof Boolean) {
115 ok = ((Boolean) result).booleanValue();
116 } else {
117 throw new ApiException("Expected Boolean result but got "+ result.getClass().getName());
118 }
119 if (!ok) throw new ApiException("Operation failed");
120 } catch (XmlRpcException e) {
121 throw new ApiException(e);
122 }
123 }
124
125 /***
126 * Returns information about all the blogs a given user is a member of.
127 *
128 * @return
129 * @throws IOException
130 * @throws ApiException
131 */
132 public Collection getUsersBlogs() throws IOException, ApiException {
133 XmlRpcClient client = new XmlRpcClient(url);
134 Vector params = new Vector();
135 params.addElement(appkey);
136 params.addElement(username);
137 params.addElement(password);
138 Collection blogs = null;
139 try {
140 Object result = client.execute("blogger.getUsersBlogs", params);
141 Vector list;
142 if (result instanceof Vector) {
143 list = (Vector) result;
144 } else {
145 throw new ApiException("Expected Vector result but got "+ result.getClass().getName());
146 }
147 blogs = new ArrayList(list.size());
148 Iterator i = list.iterator();
149 while (i.hasNext()) {
150 Object o = i.next();
151 if (!(o instanceof Map)) {
152 throw new ApiException("Expected Map but got "+ o.getClass().getName());
153 }
154 Map map = (Map) o;
155 BloggerBlog blog = new BloggerBlog((String) map.get("blogid"));
156 blog.setName((String) map.get("blogName"));
157 blog.setUrl(new URL((String) map.get("url")));
158 blogs.add(blog);
159 }
160 } catch (XmlRpcException e) {
161 throw new ApiException(e);
162 }
163 return blogs;
164 }
165
166 /***
167 * Returns the UserInfo object of the user set by the credentials.
168 *
169 * @return
170 * @throws ApiException
171 * @throws IOException
172 */
173 public UserInfo getUserInfo() throws ApiException, IOException {
174 XmlRpcClient client = new XmlRpcClient(url);
175 Vector params = new Vector();
176 params.addElement(appkey);
177 params.addElement(username);
178 params.addElement(password);
179 UserInfo info = null;
180 try {
181 Object result = client.execute("blogger.getUserInfo", params);
182 Map map;
183 if (result instanceof Map) {
184 map = (Map) result;
185 } else {
186 throw new ApiException("Expected Map result but got "+ result.getClass().getName());
187 }
188 info = new UserInfo((String) map.get("userid"));
189 info.setFirstName((String) map.get("firstname"));
190 info.setLastName((String) map.get("lastname"));
191 info.setNick((String) map.get("nickname"));
192 info.setEmail((String) map.get("email"));
193 info.setUrl((String) map.get("url"));
194 } catch (XmlRpcException e) {
195 throw new ApiException(e);
196 }
197 return info;
198 }
199
200 /***
201 * Retrieves a template.
202 *
203 * A blog must be set before calling this method.
204 * @param type one of the TEMPLATE_TYPE_* constants
205 * @return
206 */
207 public String getTemplate(String type) {
208
209 throw new RuntimeException("not implemented");
210 }
211
212 /***
213 * Sets a template.
214 *
215 * A blog must be set before calling this method.
216 * @param type one of the TEMPLATE_TYPE_* constants
217 * @param template
218 */
219 public void setTemplate(String type, String template) {
220
221 throw new RuntimeException("not implemented");
222 }
223 }